var $ = require("../../include/$");
var tablename = "wechat_client_service";

function new_date() {
    return (new Date()).toLocaleString();
}
module.exports = {
    post: function(req, res) {
        console.log("wechat_post：", new_date());
        var data = req.body;
        req.models[tablename].create(data, function(err, result) {
            if (err) {
                res.send(500, err);
                return;
            };
            res.send(result);
            res.end();
        });
    },
    delete: function(req, res) {
        console.log("wechat_delete：", new_date());
        var model_id = req.body.model_id;
        req.models[tablename].get(model_id, function(err, result) {
            if (err) {
                res.send(500, err);
                return;
            };
            if (!result) {
                res.send(404, "no found!");
                return;
            };
            result.remove(function(remove_err) {
                if (remove_err) {
                    res.send(500, remove_err);
                    return;
                };
                res.send("ok");
            });
        });
    },
    put: function(req, res) {
        console.log("wechat_put：", new_date());
        var model_data = req.body;
        req.models[tablename].get(model_data.id, function(err, result) {
            if (err) {
                res.send(500, err);
                return;
            };
            if (!result) {
                res.send(404, "no found!");
                return;
            };
            var key;
            for (key in model_data) {
                result[key] = model_data[key];
            };
            result.save(function(save_err, save_result) {
                if (save_err) {
                    res.send(500, save_err);
                    return;
                };
                res.send(save_result);
            });
        });
    },
    get: function(req, res) {
        console.log("wechat_get：", new_date());
        var model_id = req.query.model_id;
        req.models[tablename].get(model_id, function(err, result) {
            if (err) {
                res.send(500, err);
                return;
            };
            if (!result) {
                res.send(404, "no found!");
                return;
            };
            res.send(result);
        });
    }
}
